#include "BondDealMatrix.h"
#include "BondDealMatrixImpl.h"
#include "BondDealMatrixData.h"
#include <core/StyleMgr.h>
#include <core/ImageMgr.h>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QScrollBar>
#include "../StringResource.h"

namespace {
	const int leftWidth = 48;
	const int topHeight = 24;
	const int lineWidth = 68;
	const int lineHeight = 108;
}

BondDealMatrix::BondDealMatrix(QWidget* p)
	: QWidget(p)
{
	m_impl = new BondDealMatrixImpl(this);

	QVBoxLayout* layout = new QVBoxLayout(this);
	layout->setContentsMargins(0, 0, 0, 0);
	layout->addWidget(m_impl);

	m_scroll = new QScrollBar(Qt::Horizontal, this);
	m_scroll->hide();
	layout->addWidget(m_scroll);
	setLayout(layout);

	connect(m_scroll, &QScrollBar::valueChanged, this, &BondDealMatrix::onScrollChange);
}

void BondDealMatrix::setMatrixType(int type)
{
	if (!m_impl)return;
	m_impl->setMatrixType(type);
	resize();
}

int BondDealMatrix::getMatrixType()
{
	if (!m_impl)return 0;
	return m_impl->getMatrixType();
}

void BondDealMatrix::setMatrixData(BondDealMatrixData* pdata)
{
	if (!m_impl)return;
	m_impl->setMatrixData(pdata);
}

void BondDealMatrix::reloadData()
{
	if (!m_impl)return;
	m_impl->reloadData();
}

void BondDealMatrix::updateCell(std::map<int, int>& mapCell)
{
	if (!m_impl)return;
	m_impl->updateCell(mapCell);
}

void BondDealMatrix::updateMatrix(int type)
{
	if (!m_impl)return;
	m_impl->updateMatrix(type);
	resize();
}

void BondDealMatrix::setDiffCDC(int diffType)
{
	if (!m_impl)return;
	m_impl->setDiffCDC(diffType);
}

void BondDealMatrix::resizeEvent(QResizeEvent*)
{
	resize();
}

void BondDealMatrix::resize()
{
	if (!m_scroll || !m_impl)return;
	int range, page;
	QRect r = rect();
	m_impl->getScrollRange(r, range, page);
	if (range > 0 && page > 0 && range > page)
	{
		m_scroll->setRange(0, range - page);
		m_scroll->setValue(0);
		m_scroll->show();
	}
	else
		m_scroll->hide();

	m_impl->resize(0);
}

void BondDealMatrix::onScrollChange(int pos)
{
	m_impl->resize(pos);
}